/*
 * StartApplication.java
 *
 * Created on October 8, 2006, 12:15 AM
 */

package org.sunspotworld;

import com.sun.spot.peripheral.Spot;
import org.sunspotworld.sensortasks.TimeKeeper;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import com.sun.spot.util.*;
import com.sun.squawk.VM;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.sunspotworld.datawrappers.DataWrapperVector;
import org.sunspotworld.power.PowerShutdownListener;
import org.sunspotworld.protocols.MyProtocols;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {
    private static final int SECONDS = 1000;
    private static final long MINUTES = 60000;
    private boolean shutdown = true;
    
    private static final long BEACON_SAMPLE = 0*MINUTES + 5*SECONDS;
    private static final long LIGHT_SAMPLE = 0*MINUTES + 5*SECONDS;
    private static final long TEMP_SAMPLE = 0*MINUTES + 5*SECONDS;
   // private static final long REPORT_SAMPLE = 0*MINUTES + 5*SECONDS;
    private static final long POLL_INTERVAL = 0*MINUTES + 5*SECONDS;
    private static final long POWER_CHECK = 1000;
    private static final long FRAME_SIZE = 0*MINUTES + 10*SECONDS;
    private static StartApplication instance;
    
    public static MIDlet getMIDlet() {
        return instance;
    }
    
    protected void startApp() throws MIDletStateChangeException {
        System.out.println("Hello, world");
        instance = this;
        MyProtocols.registerMyProtocols();
        System.out.println("Protocols registered");
        
        Spot.getInstance().getSleepManager().enableDeepSleep();
        ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs();
        TimeKeeper.getInstance().reset();
        TimeKeeper.getInstance().setFrameSize(FRAME_SIZE);
        //ReportTask report = new ReportTask(REPORT_SAMPLE);
        //report.setRepeat(true);
        PollMobileTask poll = new PollMobileTask(POLL_INTERVAL);
        poll.setRepeat(true);
        BeaconTask beacon = new BeaconTask(BEACON_SAMPLE);
        beacon.setRepeat(true);
        BeaconTask beacon2 = new BeaconTask(BEACON_SAMPLE);
        beacon2.setRepeat(true);
        LightTask light = new LightTask(LIGHT_SAMPLE);
        light.setRepeat(true);
        TemperatureTask temp = new TemperatureTask(TEMP_SAMPLE);
        temp.setRepeat(true);
        PowerShutdownListener power = new PowerShutdownListener(POWER_CHECK);
        power.setRepeat(true);
        ISwitch[] switches = EDemoBoard.getInstance().getSwitches();
        if (switches[0].isOpen() || switches[1].isOpen()) {
            if (switches[0].isClosed() || switches[1].isClosed() || Spot.getInstance().getPersistentProperty("Ranging") == null) {
                Spot.getInstance().setPersistentProperty("Ranging", "true");
                RebootCounter.getInstance().reset();
                DataWrapperVector.getInstance().clearVector();
            }
        } else {
            RebootCounter.getInstance().reset();
            DataWrapperVector.getInstance().clearVector();
            Spot.getInstance().setPersistentProperty("Ranging", "false");
        }
        System.out.println("Reboots: " + RebootCounter.getInstance().getCount());
        if (Spot.getInstance().getPersistentProperty("Ranging").equals("true")) {
            for (int i=0; i<3; i++) {
                leds[i].setColor(LEDColor.GREEN);
                leds[i].setOn();
            }
        } else {
            for (int i=0; i<3; i++) {
                leds[i].setColor(LEDColor.BLUE);
                leds[i].setOn();
            }
        }
        Utils.sleep(1000);
        for (int i=0; i<3; i++) {
            leds[i].setOff();
        }
        /*Comment out for data dump
        TimeKeeper.getInstance().schedule(beacon);
        TimeKeeper.getInstance().schedule(light);
        TimeKeeper.getInstance().schedule(temp);
        TimeKeeper.getInstance().schedule(report);
        //*/
        TimeKeeper.getInstance().schedule(poll);
        TimeKeeper.getInstance().schedule(power);
        /*For double beacons of other sensing tasks
        TimeKeeper.getInstance().skipInterval(9*MINUTES + 39*SECONDS);
        TimeKeeper.getInstance().schedule(beacon2);
        //*/
    }

    protected void pauseApp() {
        // This will never be called by the Squawk VM
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        if (shutdown) {
            shutdown = false;
            RebootCounter.getInstance().increment();
            VM.stopVM(1);
        }
    }
}
